﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Business;
using AMM.Model;
using AMM.Model.Business;

namespace AMM.Business.Business {
    public class CustomerProcessor : BaseProcessor {

        private int _pageSize;
        private CustomerRepository _customerRepository;

        public CustomerProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._customerRepository = new CustomerRepository(pageSize);
        }

        public Customer FetchCustomerById(int customerId) {
            try {
                return this._customerRepository.FetchCustomerById(customerId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定客户资料失败！");
            }
        }

        public List<Customer> FetchCustomerList() {
            try {
                return this._customerRepository.FetchCustomerList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取客户列表失败！");
            }
        }

        public List<CustomerViewModel> FetchCustomerViewModelList(CustomerSearchModel searchModel, int pageIndex = 1) {
            try {
                List<Customer> resultList = this._customerRepository.FetchCustomerList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new CustomerViewModel() {
                        Key = x.CustomerId,
                        Code = x.Code,
                        Name = x.Name,
                        Address = x.Address,
                        Country = x.Country,
                        Zipcode = x.Zipcode,
                        Linkman1 = x.Linkman1,
                        Linkman2 = x.Linkman2,
                        Phone1 = x.Phone1,
                        Phone2 = x.Phone2,
                        Fax = x.Fax,
                        Email = x.Email,
                        IsEnabled = x.IsEnabled,
                        Website = x.Website,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取客户列表失败！");
            }
        }

        public void Insert(Customer customer) {
            try {
                customer.CustomerId = Guid.NewGuid().GetHashCode();
                customer.CreatedDatetime = DateTime.Now;
                this._customerRepository.Insert(customer);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新客户失败！");
            }
        }

        public void Update(Customer customer) {
            try {
                Customer oldCustomer = this.FetchCustomerById(customer.CustomerId);
                if (!oldCustomer.RowVersion.SequenceEqual(customer.RowVersion)) {
                    throw this.BuildException("更新客户失败，数据不一致性错误！");
                }
                customer.ModifiedDatetime = DateTime.Now;
                this._customerRepository.Update(customer);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新客户失败！");
            }
        }

        public void Delete(Customer customer) {
            try {
                this._customerRepository.Delete(customer);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除客户失败！");
            }
        }

        public void Delete(params int[] customerIds) {
            try {
                this._customerRepository.DeleteMultipleCustomer(customerIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除客户失败！");
            }
        }
    }
}
